
package orca;

import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;

public abstract class OrcaButton extends OrcaComponent
{
	private static final int MARGIN = 3;
	private final Font font;
	private final Rectangle frame;

	private String label;

	public OrcaButton()
	{
		this.frame = new Rectangle();
		this.font = new Font(OrcaContext.FONT_TYPE, Font.PLAIN, 18);
	}

	public boolean setLabel(String label)
	{
		boolean ok = true;
		if (ok)
		{
			this.label = label;
		}

		return ok;
	}

	public String getLabel()
	{
		return this.label;
	}

	public void setLocation(int x, int y)
	{
		this.frame.setLocation(x, y);
	}

	public void setSize(int width, int height)
	{
		this.frame.setSize(width, height);
	}

	public boolean contains(Point p)
	{
		return this.frame.contains(p);
	}

	public int getLocX()
	{
		return this.frame.x;
	}

	public int getLocY()
	{
		return this.frame.y;
	}

	public int getWidth()
	{
		return this.frame.width;
	}

	public int getHeight()
	{
		return this.frame.height;
	}

	public int getLabelX()
	{
		return this.frame.x + 10;
	}

	public int getLabelY()
	{
		return this.frame.y + this.frame.height / 2;
	}

	public abstract void action();

	@Override
	void setBounds(int x, int y, int width, int height)
	{
		final int doubleMargin = MARGIN * 2;

		x += MARGIN;
		y += MARGIN;
		width -= doubleMargin;
		height -= doubleMargin;

		this.frame.setBounds(x, y, width, height);
	}

	@Override
	void refresh(Graphics2D g2d)
	{
		g2d.setColor(this.getBackground());
		g2d.fill(this.frame);

		g2d.setColor(this.getForeground());
		g2d.draw(this.frame);

		g2d.drawString(this.label, this.frame.x + 4, this.frame.y + this.frame.height / 2);
	}

	@Override
	public void mouseClicked(int button, int x, int y)
	{
		if (this.frame.contains(x, y))
		{
			System.out.println("Action");
			this.action();
		}
	}

	@Override
	public void mousePressed(int button, int x, int y)
	{

	}

	@Override
	public void mouseReleased(int button, int x, int y)
	{
	}
}
